package com.wyth.lottery.utils;

import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.sdsmdg.tastytoast.TastyToast;
import com.wyth.lottery.R;
import com.wyth.lottery.com.Constants;


public class ToastUtil {
    private static TastyToast tastyToast;
    private static Toast toast;
    public static boolean mToastIsShow = false;

    /**
     * 自定义Toast
     *
     * @param text         提示内容
     * @param durationTime 显示时间 0:3s   1:5s
     */
    public static void makeToast(String text, final int durationTime) {
        toast = new Toast(MyApplication.getContext());
        View view = LayoutInflater.from(MyApplication.getContext()).inflate(R.layout.select_toast_layout, null);
        toast.setView(view);
        TextView textView = (TextView) view.findViewById(R.id.select_toast_layout);
        textView.setText(text);
        toast.setGravity(Gravity.CENTER, 0, 0);
        if (durationTime == Toast.LENGTH_SHORT) {
            toast.setDuration(Toast.LENGTH_SHORT);
        } else {
            toast.setDuration(Toast.LENGTH_LONG);
        }
        if (!mToastIsShow) {
            mToastIsShow = true;
            toast.show();
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        if (durationTime == Toast.LENGTH_SHORT) {
                            Thread.sleep(3000);
                        } else {
                            Thread.sleep(5000);
                        }
                        mToastIsShow = false;
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                }
            }).start();
        }
    }


    public static void showShortToast(Context context, String text) {
        if (toast == null) {
            toast = Toast.makeText(context, text, Toast.LENGTH_SHORT);
        } else {
            toast.setText(text);
        }
        toast.show();
    }

    public static void showLongToast(Context context, String text) {
        if (toast == null) {
            toast = Toast.makeText(context, text, Toast.LENGTH_LONG);
        } else {
            toast.setText(text);
        }
        toast.show();
    }

    public static void showToast(String text) {
        if (toast == null) {
            toast = Toast.makeText(Constants.context, text, Toast.LENGTH_SHORT);
        } else {
            toast.setText(text);
        }
        toast.show();
    }

    public static void show(Context context, String text) {
        Toast.makeText(context, text, Toast.LENGTH_SHORT).show();
    }

    public static void syccessShow(Context context, String text) {
        tastyToast.makeText(context, text, TastyToast.LENGTH_LONG, TastyToast.SUCCESS);
    }

    public static void errorShow(Context context, String text) {
        tastyToast.makeText(context, text, TastyToast.LENGTH_LONG, TastyToast.ERROR);
    }

    public static void warningShow(Context context, String text) {
        tastyToast.makeText(context, text, TastyToast.LENGTH_LONG, TastyToast.WARNING);
    }

    public static void infoShow(Context context, String text) {
        tastyToast.makeText(context, text, TastyToast.LENGTH_LONG, TastyToast.INFO);
    }

    public static void defaultShow(Context context, String text) {
        tastyToast.makeText(context, text, TastyToast.LENGTH_LONG, TastyToast.DEFAULT);
    }
}
